<?php
/**
 * Created by PhpStorm.
 * User: caoguangyang
 * Date: 2023-06-17
 * Time: 16:01
 */

namespace App\Services\Goods;
use App\Models\Goods\Category;
use App\Services\BaseServices;

class CategoryServices extends BaseServices{

    // 获取一级分类
    public function getL1List() {
        return Category::query()->where('level', "L1")
            ->where("deleted", 0)->get();
    }

    // 获取二级分类
    public function getL2ListByPid(int $pid) {
        return Category::query()->where("level", "L2")
            ->where('pid', $pid)
            ->where("deleted", 0)
            ->get();
    }

    // 根据id获取一级类目
    public function getL1ById(int $id) {
        return Category::query()->where("id", $id)
            ->where("level", "L1")
            ->where("deleted", 0)
            ->first();
    }

    /**
     * 根据id获取分类
     * @param int $id
     * @return \App\Models\BaseModel|\App\Models\BaseModel[]|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model|null
     */
    public function getCategory(int $id) {
        return Category::query()->find($id);
    }

    public function getL2ListByIds(array $ids) {
        if (empty($ids)) {
            return collect([]);
        }
        return Category::query()->whereIn('id', $ids)->get();
    }
}